package com.sinodata.bsm.cicp.finder.weblogic9;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.JmxUtil;

public class DomainFinder extends Finder {

    private static final Logger logger = Logger.getLogger(DomainFinder.class);

    /* @Override
     public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
         List resources = new ArrayList();
         String ip = host;
         String port = attrVals[0].getAttrValue();
         //        MBeanServerConnection connection = JmxUtil.getConnection(JmxUtil.WEBLOGIC, "192.168.0.211", "7001", "weblogic","weblogic1");
         ObjectName service;
         JMXConnector connector = null;
         MBeanServerConnection connection = null;
         try {
             connector = JmxUtil.getConnection(JmxUtil.WEBLOGIC, ip + ":" + port);
             connection = connector.getMBeanServerConnection();
             service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management." + "mbeanservers.domainruntime.DomainRuntimeServiceMBean");
             Object domainConfiguration = connection.getAttribute(service, "DomainConfiguration");
             String attributeNames[] = new String[] { "Name" };
             Object attrValues[] = null;
             attrValues = JmxUtil.getAttribute(connection, (ObjectName) domainConfiguration, attributeNames);
             List aRes = new ArrayList();
             ResValue res = new ResValue();
             ResAttributeValValue valValue = null;
             res.setName(attrValues[0].toString());
             res.setIp(host); // host IP address
             res.setDescript("Domain " + attrValues[0].toString());
             res.setInternalId(host + "weblogic9" + attrValues[0].toString()); // Internal ID for agent
             aRes.add(res);
             valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), attrValues[0].toString());
             aRes.add(valValue);
             valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[1].getId(), port);
             aRes.add(valValue);
             //            List oneResource = CreateOneResource(attrValues[0].toString(),port,attributes);
             resources.add(aRes);
         } catch (Exception e) {
             logger.error("failed to find weblogic9 Domain", e);
             resources = null;
         } finally {
             JmxUtil.closeConnector(connector);
         }
         return resources;
     }*/

    /*    private List CreateOneResource(String name, String port, ResAttributeValue[] attributes) {
            List aRes = new ArrayList();
            ResValue res = new ResValue();
            ResAttributeValValue valValue = null;
            res.setName(name);
            res.setIp(host); // host IP address
            res.setDescript("Domain " + name);
            res.setInternalId(host + "weblogic9" + name); // Internal ID for agent
            aRes.add(res);
            valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), name);
            aRes.add(valValue);
            valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[1].getId(), port);
            aRes.add(valValue);
            return aRes;
        }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String port = (String) params.get("PORT");
        ObjectName service;
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management." + "mbeanservers.domainruntime.DomainRuntimeServiceMBean");
            Object domainConfiguration = connection.getAttribute(service, "DomainConfiguration");
            String attributeNames[] = new String[] { "Name" };
            Object attrValues[] = null;
            attrValues = JmxUtil.getAttribute(connection, (ObjectName) domainConfiguration, attributeNames);
            String[] info = new String[5];
            list.add(info);
            info[0] = ip + "weblogic9" + attrValues[0].toString();
            info[1] = attrValues[0].toString();
            info[2] = "Domain " + attrValues[0].toString();
            info[3] = attrValues[0].toString();
            info[4] = port;
            return list;
        } catch (Exception e) {
            logger.error("failed to find weblogic9 Domain", e);
            return new ArrayList<String[]>();
        } finally {
            JmxUtil.closeConnector(connector);
        }
    }
}
